import { WeaForm } from 'comsMobx';
import { WeaLocaleProvider } from 'ecCom';
import { observable, action, runInAction } from 'mobx';
import { Base } from '../base';
import * as API from '../../apis/signlog/AppendSignApi';
import objectAssign from 'object-assign';

const { getLabel } = WeaLocaleProvider;

export default class AppendSignLogStore extends Base {
  @observable form = new WeaForm();
  @observable loading = false;
  @observable signinput = {};
  @observable showSignInput = false;

  refid = 'append';
  controlOptions = false;// 是否控制选项【主要是控制节点，控制人员】
  condition = [];
  sumitParams = {}
  coreParams = {}

  doInit = (params = {}, onCancel) => {
    params = { ...params };
    this.coreParams = params;
    this.loading = false;
    this.getSignInput();
    API.odocAppendDatas(params).then((res) => {
      const { api_status = false, conditions = [], controlOptions = false, signInputRequired = true } = res;
      if (api_status) {
        this.condition = conditions;
        this.form.initFormFields(conditions);
        this.controlOptions = controlOptions;
        this.sumitParams = { signInputRequired };
        this.loading = true;
      } else {
        const { api_message = '' } = res;
        this.showWarning(undefined, api_message, onCancel);
      }
    });
  }
  @action
  getSignInput = () => {
    this.showSignInput = false;
    API.getSignInput(this.coreParams).then((datas) => {
      runInAction(() => {
        this.showSignInput = true;
        this.signinput = datas;
      });
    });
  }

  changeSubmitParams = (datas) => {
    this.sumitParams = objectAssign({}, this.sumitParams, datas);
  }

  doSave = (workflowStores = {}, onCancel) => {
    const { globalStore = {} } = workflowStores;
    const { submitParam = {} } = globalStore;
    let params = objectAssign({}, this.form.getFormParams(), this.coreParams, submitParam, this.sumitParams);

    if (window.CK_REF && window.CK_REF[`${this.refid}Edit`]) {
      // params.remark = window.CK_REF[this.refid + "Edit"].getEditor().getData() || remark;
      params.remark = window.CK_REF[`${this.refid}Edit`].getEditor().getData();
    }
    const { signInputRequired = false } = this.sumitParams;
    if (signInputRequired && !this.checkRemark(params)) { // 签字意见必填校验失败
      this.showErrorMsg(getLabel(26013, '请填写签字意见！'));
      return;
    }
    this.form.validateForm().then((f) => {
      if (f.isValid) {
        this.loading = false;
        API.saveSignSetting(params).then((res) => {
          const { api_status = false, api_message = '' } = res;
          this.loading = true;
          if (api_status) {
            this.showSuccessMsg(getLabel(83551, '保存成功！'), 3, () => onCancel(res));
          } else {
            this.showErrorMsg(api_message);
          }
        });
      } else {
        f.showErrors();
      }
    });
  }

  getSqlWhere = (params = {}) => {
    params = { ...params, ...this.coreParams };
    this.controlOptions && API.getSqlWhere(params).then((res) => {
      const { nodes } = res;
      this.fnChangeForm({ nodes }, 'appendToNodeid');
    });
  }

	/**
	 *
	 * @param params
	 * @param key
	 */
  fnChangeForm = (params = {}, key = '') => {
    let { condition } = this;
    condition.length > 0 && condition.map((group) => {
      group.items = group.items.map((item) => {
        const { domkey = [] } = item;
        if (domkey.indexOf(key) != -1) {
          let { browserConditionParam = {} } = item;
          let { completeParams = {}, conditionDataParams = {}, dataParams = {} } = browserConditionParam;
          completeParams = { ...completeParams, ...params };
          conditionDataParams = { ...conditionDataParams, ...params };
          dataParams = { ...dataParams, ...params };
          browserConditionParam = { ...browserConditionParam, completeParams, conditionDataParams, dataParams };
          item = { ...item, browserConditionParam };
        }
        return item;
      });
      return group;
    });
    runInAction(() => {
      let datas = this.form.getFormDatas();
      datas && datas[key] && (delete datas[key]);// 删除被联动值
      this.condition = condition;
      this.form = new WeaForm();
      this.form.initFormFields(condition);
      this.form.updateFields(datas);
    });
  }

  destory = () => {
    this.form.resetForm();
  }
}
